Is this slightly scary use of "tempStringOf" viable?

I was reviewing a piece of code written by someone else that created global variables in the topmost context in order to pass buffer data out of code that was executing in an eval_ statement.

I didn't like the use of global variables in topmost for this purpose and looked at changing the eval_ script to avoid its need.  In testing feasibility only, I wrote and tested the following code which uses tempStringOf in a (to my mind) somewhat scary way - but appears to work.

So, first the code, then the explanation:

 

int Chk = LeakCheck
Buffer dxlString = create
dxlString += "print \"Running in eval\\n\"\n"
dxlString += "Buffer theErrors_ = create\n"
dxlString += "theErrors_ += \"A constructed string made via a buffer\"\n"
dxlString += "return_(tempStringOf(theErrors_))\n"
Buffer result = create
result = eval_(tempStringOf(dxlString))
delete dxlString
print tempStringOf result
delete result
print "\n" (LeakCheck()-Chk) ""

I should first say that the "LeakCheck" routine is basically Mathias's leak checking code slightly rewritten and renamed - but functionally pretty much the same.

The first slightly scary use of tempStringOf occurs when passing the script (created in a buffer) to the eval_ perm.  My argument is that, because the buffer is retained until after the eval_ has completed,  this should be pretty safe usage.

The second assumption I have made is that when the eval_ terminates, all data created in the evaluation of the script is discarded except the string that is being returned via the return_ perm.  Which means I can get away with not deleting the buffer I created in the evaluation.  This is indeed an oft-quoted reason to use eval_: to avoid having to worry about memory leaks.

The third assumption (and second more scary use of tempStringOf occurs when I assign the result of the evaluation to a buffer.  What happens is that the return_ will process a string pointer into the first buffer (in the evaluated script) and copy the string content into the second buffer.  All this without (hopefully) placing an actual string into the string table. 

This appears to work because (as far as can be determined) the destruction of the evaluation's memory allocations does not actually occur until AFTER the return_ and eval_ pair have completed!

Incidentally, if I did actually delete the first buffer in the evaluated script, then things would go pear-shaped as should be expected.  This is why I am relying on the semantics of eval_ to discard all the evaluated script's memory - thus ultimately discarding the first buffer.  However, the code as it stands is also relying on the likelihood that the discarding occurs after the assignment of the eval_ result!

Try the code (you may have to remove the two LeakCheck lines) and see for yourself.

Viable?  Or too much "Voodoo"?

 

 

p.s.  HINT: It is actually line 6 in the code that is potentially contentious, not line 8 or line 10

        Also, the code may be depending on the fact that the return_ is the last statement in the evaluated script.

 

 


AlexTidmarsh - Tue May 19 23:54:58 EDT 2015

Re: Is this slightly scary use of "tempStringOf" viable?
AlexTidmarsh - Wed May 20 00:03:34 EDT 2015

I can already hear one response to this (because it is what I would suggest), so I will deal with that now:

The eval_ perm returns a string - so it could be argued that a string is created anyway.

However, I would counter this with the fact that the return_ perm expects a string, and would therefore pass this as the result of the eval_ without wasting time and memory creating a duplicate.  Thus if it gets a string pointer via tempStringOf, it most likely passes that pointer through.

This surmise is borne out by the fact that actually deleting the buffer in the evaluated script makes it all go pear-shaped - which is what would happen if the pointer was passed through and still pointed into the now expressly discarded buffer.  It would suggest that no string is created on the fly.

 

Re: Is this slightly scary use of "tempStringOf" viable?
AlexTidmarsh - Wed May 20 00:29:52 EDT 2015

Incidentally, the result output I get from the dxl-edit window is:
 

Running in eval

A constructed string made via a buffer

0

 

Re: Is this slightly scary use of "tempStringOf" viable?
AlexTidmarsh - Wed May 20 00:42:11 EDT 2015

As an afterthought, a real test of this would be to load the script directly from a file into a buffer - thus not polluting the string table with anything at all.  And then run some code that displays the string table after executing the eval_.

Unfortunately I posses no library code to do either - so if anyone has such code... Mathias? ...

:-)

 

Re: Is this slightly scary use of "tempStringOf" viable?
Mathias Mamsch - Wed May 20 09:59:43 EDT 2015

AlexTidmarsh - Wed May 20 00:42:11 EDT 2015

As an afterthought, a real test of this would be to load the script directly from a file into a buffer - thus not polluting the string table with anything at all.  And then run some code that displays the string table after executing the eval_.

Unfortunately I posses no library code to do either - so if anyone has such code... Mathias? ...

:-)

 

Hi Alex,

I am pretty sure that this approach does not save you from string table littering. The return_ statement will very likely produce a string table entry, the question is, if its permanent or not. You can test this however:

(String Table Code is here: https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014886977&ps=25)

#include <D:/petools.inc>
#include <D:/stringTable.inc>

printStringTable()

int i; for (i = 0; i < 100; i++) {
    Buffer buf = create(); 
      buf += "Buffer z = create()\n"
      buf += "int i; for (i = 0; i < 100; i++) {\n"
      buf += "z += charOf ((random 64) + 64)\n"
      buf += "}\n"
//       buf += "return_ tempStringOf z"   // Comment me in to get string table littering
      eval_ tempStringOf buf; 
      delete buf
}


printStringTable()

As you can see, inside the val we are constructing 100char random strings inside a buffer without any string table littering.

Once you comment in the the return_ statement the strings will be persisted in the string table. You can see that with every start of the script the string table entries will rise by 100. Therefore if you want to pass a string out from an eval_ context you should use a static eval code with a buffer reference. It is perfectly allowed to write to a parent DXL context buffer from a child context. You will not even need a global variable for this:

#include <D:/petools.inc>
#include <D:/stringTable.inc>

printStringTable()

int i; for (i = 0; i < 100; i++) {
      Buffer buf = create(); 
      Buffer result = create(); 
      buf += "Buffer z = addr_ "
      buf += ((addr_ result) int) ""
      buf += "\nint i; for (i = 0; i < 100; i++) {\n"
      buf += "z += charOf ((random 64) + 64)\n"
      buf += "}\n"
      eval_ tempStringOf buf; 
      delete buf
      print (tempStringOf result); 
      delete result
}


printStringTable()

And if you want to save yourself the time and the non-permanent string table entries for converting the buffer address to a string, then you should use static DXL code and use references to access the buffers. This way you will not need to assemble the code inside the buffer every time you call the function:

https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014942749

(Note that in that example you would not need to pass the buffer into the eval context by reference).

No need to fear global variables in DXL. They might be discouraged for other programming languages but for DXL they are an imperative.

Regards, Mathias

 

Re: Is this slightly scary use of "tempStringOf" viable?
AlexTidmarsh - Thu May 21 01:40:52 EDT 2015

Mathias Mamsch - Wed May 20 09:59:43 EDT 2015

Hi Alex,

I am pretty sure that this approach does not save you from string table littering. The return_ statement will very likely produce a string table entry, the question is, if its permanent or not. You can test this however:

(String Table Code is here: https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014886977&ps=25)

#include <D:/petools.inc>
#include <D:/stringTable.inc>

printStringTable()

int i; for (i = 0; i < 100; i++) {
    Buffer buf = create(); 
      buf += "Buffer z = create()\n"
      buf += "int i; for (i = 0; i < 100; i++) {\n"
      buf += "z += charOf ((random 64) + 64)\n"
      buf += "}\n"
//       buf += "return_ tempStringOf z"   // Comment me in to get string table littering
      eval_ tempStringOf buf; 
      delete buf
}


printStringTable()

As you can see, inside the val we are constructing 100char random strings inside a buffer without any string table littering.

Once you comment in the the return_ statement the strings will be persisted in the string table. You can see that with every start of the script the string table entries will rise by 100. Therefore if you want to pass a string out from an eval_ context you should use a static eval code with a buffer reference. It is perfectly allowed to write to a parent DXL context buffer from a child context. You will not even need a global variable for this:

#include <D:/petools.inc>
#include <D:/stringTable.inc>

printStringTable()

int i; for (i = 0; i < 100; i++) {
      Buffer buf = create(); 
      Buffer result = create(); 
      buf += "Buffer z = addr_ "
      buf += ((addr_ result) int) ""
      buf += "\nint i; for (i = 0; i < 100; i++) {\n"
      buf += "z += charOf ((random 64) + 64)\n"
      buf += "}\n"
      eval_ tempStringOf buf; 
      delete buf
      print (tempStringOf result); 
      delete result
}


printStringTable()

And if you want to save yourself the time and the non-permanent string table entries for converting the buffer address to a string, then you should use static DXL code and use references to access the buffers. This way you will not need to assemble the code inside the buffer every time you call the function:

https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014942749

(Note that in that example you would not need to pass the buffer into the eval context by reference).

No need to fear global variables in DXL. They might be discouraged for other programming languages but for DXL they are an imperative.

Regards, Mathias

 

Thanks for this, Mathias.

I shall have to spend some time with this to get my head around it.

As for the comment about global variables, I understand what you mean - but the general discouragement of global variables is not anything to do with language - they are bad practice in a language-independent way.  The fact that DXL kind of forces you to use them IMHO is just another measure of how poorly designed the language is. :-)   

p.s. as a lifelong practitioner (30 years worth) of programming embedded systems with C, I have come to truly hate that language too.  But we all program where the money takes us.  -  Delphi (extended Pascal) is the language I hate the least. :-)    It's a productivity thing.   I can write a thousand lines of debugged working Pascal code in the time it takes to write 100 lines of debugged working C or 20 lines of DXL that might just about work.  (And as the question is begged: 1,000 lines of Pascal?  three to five days, probably).  ok. there may be a small bit of proportional exaggeration for effect - but the real measure is the deepness of the sigh I emit when I realise which language I am going to have to use for a job. 

 

But back to the point... Temporary string table littering?  Ok, that sounds like a good thing.

 

 

 

Re: Is this slightly scary use of "tempStringOf" viable?
AlexTidmarsh - Thu May 21 02:17:37 EDT 2015

AlexTidmarsh - Thu May 21 01:40:52 EDT 2015

Thanks for this, Mathias.

I shall have to spend some time with this to get my head around it.

As for the comment about global variables, I understand what you mean - but the general discouragement of global variables is not anything to do with language - they are bad practice in a language-independent way.  The fact that DXL kind of forces you to use them IMHO is just another measure of how poorly designed the language is. :-)   

p.s. as a lifelong practitioner (30 years worth) of programming embedded systems with C, I have come to truly hate that language too.  But we all program where the money takes us.  -  Delphi (extended Pascal) is the language I hate the least. :-)    It's a productivity thing.   I can write a thousand lines of debugged working Pascal code in the time it takes to write 100 lines of debugged working C or 20 lines of DXL that might just about work.  (And as the question is begged: 1,000 lines of Pascal?  three to five days, probably).  ok. there may be a small bit of proportional exaggeration for effect - but the real measure is the deepness of the sigh I emit when I realise which language I am going to have to use for a job. 

 

But back to the point... Temporary string table littering?  Ok, that sounds like a good thing.

 

 

 

Actually, I can already here my friend and colleague Peter saying - if you stop fighting the language your productivity could go up.  To which I of course would counter:   yey! 1,000 lines of DXL that works... for now... 

Re: Is this slightly scary use of "tempStringOf" viable?
Mathias Mamsch - Mon May 25 04:45:04 EDT 2015

AlexTidmarsh - Thu May 21 01:40:52 EDT 2015

Thanks for this, Mathias.

I shall have to spend some time with this to get my head around it.

As for the comment about global variables, I understand what you mean - but the general discouragement of global variables is not anything to do with language - they are bad practice in a language-independent way.  The fact that DXL kind of forces you to use them IMHO is just another measure of how poorly designed the language is. :-)   

p.s. as a lifelong practitioner (30 years worth) of programming embedded systems with C, I have come to truly hate that language too.  But we all program where the money takes us.  -  Delphi (extended Pascal) is the language I hate the least. :-)    It's a productivity thing.   I can write a thousand lines of debugged working Pascal code in the time it takes to write 100 lines of debugged working C or 20 lines of DXL that might just about work.  (And as the question is begged: 1,000 lines of Pascal?  three to five days, probably).  ok. there may be a small bit of proportional exaggeration for effect - but the real measure is the deepness of the sigh I emit when I realise which language I am going to have to use for a job. 

 

But back to the point... Temporary string table littering?  Ok, that sounds like a good thing.

 

 

 

Regarding temporary string table littering:

I realized, that strings that were never passed to a perm are not persisted permanently in the string table but only temporary.

If you do code like:

int i; for (i = 0; i < 100; i++) {
   s = ((random 10000)*i)  ""
}

This will also create string table entries, but they will be cleared when the DXL ends. That is what I call "temporary string table littering".

Regards, Mathias
 

Re: Is this slightly scary use of "tempStringOf" viable?
AlexTidmarsh - Tue May 26 04:52:25 EDT 2015

Mathias Mamsch - Mon May 25 04:45:04 EDT 2015

Regarding temporary string table littering:

I realized, that strings that were never passed to a perm are not persisted permanently in the string table but only temporary.

If you do code like:

int i; for (i = 0; i < 100; i++) {
   s = ((random 10000)*i)  ""
}

This will also create string table entries, but they will be cleared when the DXL ends. That is what I call "temporary string table littering".

Regards, Mathias
 

I'm sorry?  Do my ears/eyes deceive me?

Are you saying that anything assembled with strings is not permanent unless those strings individually go to a perm?
Thus, 

       s = "Hello for the " count "th time";  print s;

actually only creates one permanent string, because print is clearly a perm, but so does:

      s = "Hello"
      s = s "for the "
      s = s count
      s = s "th time"
      print s

That really does put a new spin on my previous understanding!   :-)
 

(Unless, of course, this is a game of catch-22 and the '=' is one of those perms!)

p.s.  Isn't  "return_" a perm?   Not sure this is always true then...
   

 

(Confused face)   ...define "perm" in this context, please... does it exclude operators and their overloads, or is it an even smaller subset than that?


 

Re: Is this slightly scary use of "tempStringOf" viable?
Mathias Mamsch - Tue May 26 07:41:37 EDT 2015

AlexTidmarsh - Tue May 26 04:52:25 EDT 2015

I'm sorry?  Do my ears/eyes deceive me?

Are you saying that anything assembled with strings is not permanent unless those strings individually go to a perm?
Thus, 

       s = "Hello for the " count "th time";  print s;

actually only creates one permanent string, because print is clearly a perm, but so does:

      s = "Hello"
      s = s "for the "
      s = s count
      s = s "th time"
      print s

That really does put a new spin on my previous understanding!   :-)
 

(Unless, of course, this is a game of catch-22 and the '=' is one of those perms!)

p.s.  Isn't  "return_" a perm?   Not sure this is always true then...
   

 

(Confused face)   ...define "perm" in this context, please... does it exclude operators and their overloads, or is it an even smaller subset than that?


 

well - most perms? I am not entirely sure - After all the ::.. (concatenation operator) is also a perm, as is "=" - no these perms do not produce permanent string table entries.

Why or which perms exactly produce permanent string table entries - I can only speculate, but I was wrong lots of times with speculations so I would not even try. "Print" does not by the way, but from trying most other relevant perms do. If you find a "scheme" or something, then let us know.

Regards, Mathias

Re: Is this slightly scary use of "tempStringOf" viable?
AlexTidmarsh - Wed May 27 14:09:11 EDT 2015

Mathias Mamsch - Tue May 26 07:41:37 EDT 2015

well - most perms? I am not entirely sure - After all the ::.. (concatenation operator) is also a perm, as is "=" - no these perms do not produce permanent string table entries.

Why or which perms exactly produce permanent string table entries - I can only speculate, but I was wrong lots of times with speculations so I would not even try. "Print" does not by the way, but from trying most other relevant perms do. If you find a "scheme" or something, then let us know.

Regards, Mathias

Print doesn't !?!

Suddenly, all my intense paranoia about the string tables falls away ...

So 
 

     s = "Hello for the " count "th time";  print s;
 

only pollutes the string table temporarily then.
And by this I assume temporarily means until the current DXL context ends?
Or until a subroutine frame is discarded, even?  (which would be even better of course) - but no, even I know better than that...
 

Re: Is this slightly scary use of "tempStringOf" viable?
llandale - Wed May 27 20:45:20 EDT 2015

Didn't read the replies, or in fact your entire post.

The string returned from "tempStringOf" is reliable only while you subsequently neither replace the contents nor delete the source Buffer.  I'm guessing you can safely ADD more string to the Buffer.

The "tempStringOf" for the eval_ seems reasonable.  I think, and I repeat <think> it is stable enough for the duration of the eval_ all because the Buffer in which it resides is continuing to exist and is not modified inside the eval_.

The "tempStringOf" of the Results is reasonable only because you are disposing of it immediately; printing it and you are done. 
 

This in fact is the standard use of "tempStringOf": displose of the string and move on.

-Louie

Re: Is this slightly scary use of "tempStringOf" viable?
AlexTidmarsh - Wed May 27 21:34:11 EDT 2015

llandale - Wed May 27 20:45:20 EDT 2015

Didn't read the replies, or in fact your entire post.

The string returned from "tempStringOf" is reliable only while you subsequently neither replace the contents nor delete the source Buffer.  I'm guessing you can safely ADD more string to the Buffer.

The "tempStringOf" for the eval_ seems reasonable.  I think, and I repeat <think> it is stable enough for the duration of the eval_ all because the Buffer in which it resides is continuing to exist and is not modified inside the eval_.

The "tempStringOf" of the Results is reasonable only because you are disposing of it immediately; printing it and you are done. 
 

This in fact is the standard use of "tempStringOf": displose of the string and move on.

-Louie

I have now been using this return mechanism in a real system since the date of the original post, and no-one has noticed any faults occurring, so I'm guessing the consensus is that, yes, it is viable - taking your point about its lifetime into account.   After the discussion with Mathias, the question now becomes: is it actually beneficial?  (Beyond the avoidance of using global variables and/or the use of hard-to-read constructs like the one Mathias demonstrated - and I have also used in the past, incidentally).

Yes: the assignment direct from the eval_ result into a new buffer effectively ends the tempStringOf lifecycle started within the return_ and passed out of the eval_ there and then:  noting in particular that there are no more statements in the evaluated script after the return_ and no other use made of the eval_ result other than to immediately put it into a buffer.  So on the face of it, it (sort of) meets the limitations of use you have stated.  Usage thus far has certainly not raised any errors.

From what Mathias has said, any temporary string table pollution caused during the evaluated script should also end immediately with the end of the eval_ context (along with any undeleted buffers, etc).  So: not only does it appear to work as a transfer mechanism, it is possibly beneficial: noting that the "usual" way of doing this is for the "return_" to process an actual string (permanent? from the string table?) rather than a pointer into a buffer (tempStringOf).  Although the only way to know if it is beneficial is to know whether the "normal" way pollutes the string table (temporarily or permanently) in the memory space (context) of the caller of the eval_.   Mathias has effectively begged that question.  In theory, the tempStringOf  method  I've proposed shouldn't even pollute it temporarily - but it is beginning to look like it does.  Which means my assumption about the context destruction after the final assignment may be slightly wrong - and is being "masked" by the use of a temporary string table entry being created in the caller's memory space as part of the "tail-end" work of the eval_ perm.

-Alex

 P.S. Louie, if you do read the whole thread thus far, you'll notice the bit about deleting the buffer within the script and its effect on the preceding return_ and eventual final eval_ result, which suggested that the pointer is passed through "as-is" without putting it into a string table, temporarily or permanently. It also appeared to me that the final destruction of the memory space (i.e. context) of the evaluated script occurs just after the eval's final assignment.  If it didn't, then this trick with tempStringOf wouldn't be working - in the same way that it doesn't if I expressly delete the buffer in the script.  However - the question is begged by Mathias whether the transfer between return_ and eval_ at the very point of destroying the context is in fact misleading me because it DOES momentarily create a temporary string table entry - i.e. one that would be corrupted by buffer deletion, but NOT by context deletion - ergo a temporary string table entry is created in the memory space of the eval caller by the eval perm itself.  i.e. the sequence is in fact: a) create a temp string table entry, b) destroy the eval context, c) do the assignment.   Rather than a) do the assignment, b) destroy the eval context.   This does make sense.

To summarize:
1. It appears it is viable, but...
2. It would be useful, if it can be shown that it does not pollute the context of the eval caller at all, or...
3. It would be useful, if it can be shown that pollution is only temporary, but the "normal" method would cause permanent pollution.

If I understand Mathias's test results correctly, 1 is true, 2 if false, 3 is not yet answered ...but I'm still trying to get my head around what Mathias's test-example and string table tool are actually telling me :-)

I am slightly suspicious of his results, because he is not actually testing the original example: In his test he is using a buffer that is effectively in the eval - caller's memory space, which may change things (or not).  Or is it?  Also I'm not quite sure how you determine from the tool's output numbers whether the pollution is temporary or permanent (I'm a bit slow on the uptake, like that). So I guess the jury is still out on the question of usefulness - beyond the fact that it is fairly easy to read and understand the code and doesn't require globals, that is.

 

P.P.S.   Sanity check: the original requirement is to create buffered data inside an eval, and then return it from the eval as buffered data, thus avoiding string table pollution.  Easily done with global variables in the topmost context (which can be seen from within an eval), but not good form:  Top-most namespace should be treated with "oodles" of respect, and not polluted with loads of arbitrarily named global variables created by Tom, Dick and Harry for their own nefarious purposes.

 

Re: Is this slightly scary use of "tempStringOf" viable?
llandale - Thu May 28 16:07:47 EDT 2015

AlexTidmarsh - Wed May 27 21:34:11 EDT 2015

I have now been using this return mechanism in a real system since the date of the original post, and no-one has noticed any faults occurring, so I'm guessing the consensus is that, yes, it is viable - taking your point about its lifetime into account.   After the discussion with Mathias, the question now becomes: is it actually beneficial?  (Beyond the avoidance of using global variables and/or the use of hard-to-read constructs like the one Mathias demonstrated - and I have also used in the past, incidentally).

Yes: the assignment direct from the eval_ result into a new buffer effectively ends the tempStringOf lifecycle started within the return_ and passed out of the eval_ there and then:  noting in particular that there are no more statements in the evaluated script after the return_ and no other use made of the eval_ result other than to immediately put it into a buffer.  So on the face of it, it (sort of) meets the limitations of use you have stated.  Usage thus far has certainly not raised any errors.

From what Mathias has said, any temporary string table pollution caused during the evaluated script should also end immediately with the end of the eval_ context (along with any undeleted buffers, etc).  So: not only does it appear to work as a transfer mechanism, it is possibly beneficial: noting that the "usual" way of doing this is for the "return_" to process an actual string (permanent? from the string table?) rather than a pointer into a buffer (tempStringOf).  Although the only way to know if it is beneficial is to know whether the "normal" way pollutes the string table (temporarily or permanently) in the memory space (context) of the caller of the eval_.   Mathias has effectively begged that question.  In theory, the tempStringOf  method  I've proposed shouldn't even pollute it temporarily - but it is beginning to look like it does.  Which means my assumption about the context destruction after the final assignment may be slightly wrong - and is being "masked" by the use of a temporary string table entry being created in the caller's memory space as part of the "tail-end" work of the eval_ perm.

-Alex

 P.S. Louie, if you do read the whole thread thus far, you'll notice the bit about deleting the buffer within the script and its effect on the preceding return_ and eventual final eval_ result, which suggested that the pointer is passed through "as-is" without putting it into a string table, temporarily or permanently. It also appeared to me that the final destruction of the memory space (i.e. context) of the evaluated script occurs just after the eval's final assignment.  If it didn't, then this trick with tempStringOf wouldn't be working - in the same way that it doesn't if I expressly delete the buffer in the script.  However - the question is begged by Mathias whether the transfer between return_ and eval_ at the very point of destroying the context is in fact misleading me because it DOES momentarily create a temporary string table entry - i.e. one that would be corrupted by buffer deletion, but NOT by context deletion - ergo a temporary string table entry is created in the memory space of the eval caller by the eval perm itself.  i.e. the sequence is in fact: a) create a temp string table entry, b) destroy the eval context, c) do the assignment.   Rather than a) do the assignment, b) destroy the eval context.   This does make sense.

To summarize:
1. It appears it is viable, but...
2. It would be useful, if it can be shown that it does not pollute the context of the eval caller at all, or...
3. It would be useful, if it can be shown that pollution is only temporary, but the "normal" method would cause permanent pollution.

If I understand Mathias's test results correctly, 1 is true, 2 if false, 3 is not yet answered ...but I'm still trying to get my head around what Mathias's test-example and string table tool are actually telling me :-)

I am slightly suspicious of his results, because he is not actually testing the original example: In his test he is using a buffer that is effectively in the eval - caller's memory space, which may change things (or not).  Or is it?  Also I'm not quite sure how you determine from the tool's output numbers whether the pollution is temporary or permanent (I'm a bit slow on the uptake, like that). So I guess the jury is still out on the question of usefulness - beyond the fact that it is fairly easy to read and understand the code and doesn't require globals, that is.

 

P.P.S.   Sanity check: the original requirement is to create buffered data inside an eval, and then return it from the eval as buffered data, thus avoiding string table pollution.  Easily done with global variables in the topmost context (which can be seen from within an eval), but not good form:  Top-most namespace should be treated with "oodles" of respect, and not polluted with loads of arbitrarily named global variables created by Tom, Dick and Harry for their own nefarious purposes.

 

I suspect the "useful and dependable solution"will be to

  • Move line #7 up above line #2 (original post)
  • Pass the addr_ of your "result" buffer to the eval_ code;
  • the code does some clever "Buffer theErrors_= addr_ (int addr_)" voodoo
  • you don't need any "return_" statements; except maybe if you want to pass back some small error code.
  • your line #8 must be rid of the assignment.

Back to your method:

Side note: "return_" is not the same as a "return" from a function.  The 2nd takes action, the statement after "return" cannot be executed.  "return_" simply stages the output string but does not terminate the eval_.  Thus, you could add a line to "delete(theErrors_)" at line 6.1. 
eval_ code can be structured as follows:

return_("Default Return Code")
if (I find some unlikely stuff) then { return_("Unlikely Return Code"); halt}
if (I find what I want) then return_("Desirable Return Code")

Les say this is all "mute" unless you figure to do 1000's of somewhat different huge eval_ calls with different huge results.
Lets say "useful" means reducing overall string table space in the main program; (or somehow stopping reduction of the maximum size of the string table; far harder to measure)
Lets say "Dependable" means whether this technique will always work; in this case whether the contents of "results" remains uncorrupted until you explicitly delete or replace that Buffer; no matter how long that takes.

line #8 [eval_(tempStringOf(dxlString))] is no doubt useful and dependable.  <<unless you pass the addr_ of dxlString to the eval_ code itself and let it muck around with it in a self-evolving-code way.  I think even MM would call that "voodoo">>

Your line #6 is flawed as you are creating a real string by concatenating the EOL at the end of the tempStringof.  Assuming you add line 5.5 theErrors_ += "\n" and then remove \n from the end of line 5: then line 6 [return_(tempStringOf(theErrors_))] is useful but not dependable.  I think you will find that if you do processing before line #10 (calling other functions etc) that Buffer "result" ends up with garbage.  Surely the space taken up with "theErrors_" is de-allocated with the eval_ context ends and can easily be re-used during these inserted functions.

Seems MM's clever string table space evaluator is only useful in the main program (call before eval_, call after getting the "results"; compare)  Who cares about the string table space inside eval_ if it all gets erased when finished.

I remind you that "tempStringOf" is undocumented and IBM is free to change the way it works.

-Louie

Re: Is this slightly scary use of "tempStringOf" viable?
Mathias Mamsch - Thu May 28 16:45:12 EDT 2015

AlexTidmarsh - Wed May 27 21:34:11 EDT 2015

I have now been using this return mechanism in a real system since the date of the original post, and no-one has noticed any faults occurring, so I'm guessing the consensus is that, yes, it is viable - taking your point about its lifetime into account.   After the discussion with Mathias, the question now becomes: is it actually beneficial?  (Beyond the avoidance of using global variables and/or the use of hard-to-read constructs like the one Mathias demonstrated - and I have also used in the past, incidentally).

Yes: the assignment direct from the eval_ result into a new buffer effectively ends the tempStringOf lifecycle started within the return_ and passed out of the eval_ there and then:  noting in particular that there are no more statements in the evaluated script after the return_ and no other use made of the eval_ result other than to immediately put it into a buffer.  So on the face of it, it (sort of) meets the limitations of use you have stated.  Usage thus far has certainly not raised any errors.

From what Mathias has said, any temporary string table pollution caused during the evaluated script should also end immediately with the end of the eval_ context (along with any undeleted buffers, etc).  So: not only does it appear to work as a transfer mechanism, it is possibly beneficial: noting that the "usual" way of doing this is for the "return_" to process an actual string (permanent? from the string table?) rather than a pointer into a buffer (tempStringOf).  Although the only way to know if it is beneficial is to know whether the "normal" way pollutes the string table (temporarily or permanently) in the memory space (context) of the caller of the eval_.   Mathias has effectively begged that question.  In theory, the tempStringOf  method  I've proposed shouldn't even pollute it temporarily - but it is beginning to look like it does.  Which means my assumption about the context destruction after the final assignment may be slightly wrong - and is being "masked" by the use of a temporary string table entry being created in the caller's memory space as part of the "tail-end" work of the eval_ perm.

-Alex

 P.S. Louie, if you do read the whole thread thus far, you'll notice the bit about deleting the buffer within the script and its effect on the preceding return_ and eventual final eval_ result, which suggested that the pointer is passed through "as-is" without putting it into a string table, temporarily or permanently. It also appeared to me that the final destruction of the memory space (i.e. context) of the evaluated script occurs just after the eval's final assignment.  If it didn't, then this trick with tempStringOf wouldn't be working - in the same way that it doesn't if I expressly delete the buffer in the script.  However - the question is begged by Mathias whether the transfer between return_ and eval_ at the very point of destroying the context is in fact misleading me because it DOES momentarily create a temporary string table entry - i.e. one that would be corrupted by buffer deletion, but NOT by context deletion - ergo a temporary string table entry is created in the memory space of the eval caller by the eval perm itself.  i.e. the sequence is in fact: a) create a temp string table entry, b) destroy the eval context, c) do the assignment.   Rather than a) do the assignment, b) destroy the eval context.   This does make sense.

To summarize:
1. It appears it is viable, but...
2. It would be useful, if it can be shown that it does not pollute the context of the eval caller at all, or...
3. It would be useful, if it can be shown that pollution is only temporary, but the "normal" method would cause permanent pollution.

If I understand Mathias's test results correctly, 1 is true, 2 if false, 3 is not yet answered ...but I'm still trying to get my head around what Mathias's test-example and string table tool are actually telling me :-)

I am slightly suspicious of his results, because he is not actually testing the original example: In his test he is using a buffer that is effectively in the eval - caller's memory space, which may change things (or not).  Or is it?  Also I'm not quite sure how you determine from the tool's output numbers whether the pollution is temporary or permanent (I'm a bit slow on the uptake, like that). So I guess the jury is still out on the question of usefulness - beyond the fact that it is fairly easy to read and understand the code and doesn't require globals, that is.

 

P.P.S.   Sanity check: the original requirement is to create buffered data inside an eval, and then return it from the eval as buffered data, thus avoiding string table pollution.  Easily done with global variables in the topmost context (which can be seen from within an eval), but not good form:  Top-most namespace should be treated with "oodles" of respect, and not polluted with loads of arbitrarily named global variables created by Tom, Dick and Harry for their own nefarious purposes.

 

Its not that complicated, really. Just a hint on how to read the numbers:

#include <C:/temp/petools.inc>
#include <C:/temp/stringTable.inc>

printStringTable(); 

string s = "xxx"

// will generate 100 strings: xxxa, xxxaa, xxxaaa ...
int i; for (i = 0; i < 100; i++) s = s " a "  

printStringTable(); // Here Count = ... will have increased by 100

This will give you something like:

52021                          // <<-- The number of bins inside the string table (fixed, not so interesting)
Count = 17899            // The interesting part: Number of String table entries ...
Clashes = 2763           // <<-- The number of clashes inside the string table performance)
52021                        
Count = 17999            // Note that after the loop the string table entries have increased by 100
Clashes = 2860
 

Now when you start that DXL script a second time, you will get the same numbers. That means, that after the start, the number of string table entries goes back to the number they were before the script, 17899. This is what I call temporary string table littering. And yes, inside an eval these kind of string table entries will be dismissed, when the eval_ ends. 

Now we modify our example, to generate permanent string table entries:

#include <C:/temp/petools.inc>
#include <C:/temp/stringTable.inc>

printStringTable(); 

Buffer buf = create(); buf = "xxx"; 

// will generate 100 strings: xxxa, xxxaa, xxxaaa ...
int i; for (i = 0; i < 100; i++) {
    buf += 'a';
    stringOf buf   // will make the generated string permanent in the string table
}

printStringTable(); // Here Count = ... will have increased by 100

The very first time I run the script:

Count = 17899
...
Count = 17999  // 100 entries made...

The second time I run the script, I get:

Count = 17999
...
Count = 17999  // No additional entries?

So the first thing you notice, is that the first count is at the level of where the last script ended. This means the string table entries did not go back to the original value. That means, they are permanent!

The next thing you need to notice is, that there were no additional antries created! Why? Because the strings xxxa, xxxaa, xxxaaa, ... were already inside the string table, and therefore were not added again!

So there is two very important consequences from this:

1. Whenever you try to measure your string table entries, you need to be aware, that you can only do this reliably the first time you start the script. After that your strings have been persisted, and you will not measure them to be added again! You need to restart your client, to clear the string table, before measuring again.

2. eval_ does not save you from permanent string table entries. eval_ with using return_ is much worse because it might produce permanent string table entries, where you would have had temporary ones, or none (as in your tempStringOf example). eval_ might save you from lots of temporary string table entries, while your program is running - but I find that is not useful at all. The string table will not slow your program down considerably until you come into a million strings. And having a million temporary string table entries - is something I doubt I will observe in a real use DXL script. This amount of data, almost always comes from attribute values or other data sources. I which case you will again have to deal with permanent string table entries again.

Hope that clears up this discussion - Summary: Using eval_  + return_  means more harm than use, but in most cases you will not even notice the problem, because string table slow down occurs much later than allocation leak slowdown.

Regards, Mathias